package main

import (
	"fmt"

	"ddl_checker/pkg/analyzer"
)

func main() {
	fmt.Println("=== SQL分析器对比示例 ===")
	fmt.Println()

	// 创建pg_query_go分析器
	pgQueryAnalyzer := analyzer.NewAnalyzer(analyzer.AnalyzerTypePostgreSQL, true)

	// TiDB分析器暂时无法正常初始化（需要特殊的构建标签）
	fmt.Println("注意：TiDB解析器当前需要特殊的构建标签初始化，暂时无法演示")
	fmt.Println()

	// 测试SQL语句
	testCases := []struct {
		name string
		sql  string
	}{
		{
			name: "简单的CREATE TABLE",
			sql: `CREATE TABLE users (
				id INT PRIMARY KEY,
				name VARCHAR(100) NOT NULL,
				email VARCHAR(255) UNIQUE
			);`,
		},
		{
			name: "ALTER TABLE ADD COLUMN",
			sql:  `ALTER TABLE users ADD COLUMN age INT DEFAULT 0;`,
		},
		{
			name: "带JOIN的SELECT查询",
			sql: `SELECT u.name, o.order_id
				FROM users u
				JOIN orders o ON u.id = o.user_id
				WHERE u.age > 18;`,
		},
		{
			name: "带子查询的INSERT",
			sql: `INSERT INTO active_users (id, name, created_at)
				SELECT id, name, created_at
				FROM users
				WHERE status = 'active';`,
		},
		{
			name: "多表DELETE",
			sql: `DELETE FROM user_sessions
				WHERE user_id IN (
					SELECT id FROM users WHERE account_locked = true
				);`,
		},
	}

	for _, tc := range testCases {
		fmt.Printf("=== 测试用例: %s ===\n", tc.name)
		fmt.Printf("SQL: %s\n", tc.sql)
		fmt.Println()

		// pg_query_go解析器结果
		fmt.Println("--- pg_query_go解析器结果 ---")
		pgStmt, pgErr := pgQueryAnalyzer.ParseSingle(tc.sql)
		if pgErr != nil {
			fmt.Printf("❌ 解析失败: %v\n", pgErr)
		} else {
			fmt.Printf("✅ 解析成功\n")
			fmt.Printf("语句类型: %s\n", pgStmt.StatementType)
			fmt.Printf("语法正确: %v\n", pgStmt.IsValid)
			fmt.Printf("关联表: %v\n", pgStmt.Tables)
			fmt.Printf("关联列: %v\n", pgStmt.Columns)
			fmt.Printf("操作: %v\n", pgStmt.Operations)
		}
		fmt.Println()

		fmt.Println("========================================")
		fmt.Println()
	}

	// 测试批量SQL解析
	fmt.Println("=== 批量SQL解析测试 ===")
	batchSQL := `CREATE TABLE test_table (
		id INT PRIMARY KEY,
		name VARCHAR(50)
	);

	INSERT INTO test_table (name) VALUES ('test1'), ('test2');

	SELECT * FROM test_table WHERE name LIKE 'test%';

	DROP TABLE test_table;`

	fmt.Println("--- pg_query_go批量解析 ---")
	pgBatch, pgBatchErr := pgQueryAnalyzer.ParseBatch(batchSQL, ";")
	if pgBatchErr != nil {
		fmt.Printf("❌ pg_query_go批量解析失败: %v\n", pgBatchErr)
	} else {
		fmt.Printf("✅ pg_query_go成功解析 %d 条语句\n", len(pgBatch))
		for i, stmt := range pgBatch {
			originalSQL := stmt.OriginalSQL
			if len(originalSQL) > 50 {
				originalSQL = originalSQL[:50] + "..."
			}
			fmt.Printf("  语句%d: %s (%s)\n", i+1, stmt.StatementType, originalSQL)
		}
	}

	fmt.Println("\n=== 语法验证测试 ===")
	validSQL := "SELECT * FROM users;"
	invalidSQL := "SELECT * FROM nonexistent_table WHERE syntax_error ="

	pgValid, pgMsg := pgQueryAnalyzer.ValidateSQL(validSQL)
	pgInvalid, pgInvalidMsg := pgQueryAnalyzer.ValidateSQL(invalidSQL)

	fmt.Printf("有效SQL: %s\n", validSQL)
	fmt.Printf("验证结果: %v, 消息: %s\n", pgValid, pgMsg)
	fmt.Printf("无效SQL: %s\n", invalidSQL)
	fmt.Printf("验证结果: %v, 错误信息: %s\n", pgInvalid, pgInvalidMsg)
}
